package richbar.com.richbar.activity.me;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.common.base.BaseActivity;
import com.common.base.BaseResult;
import com.common.util.LogUtil;
import com.common.util.StringUtil;
import com.common.util.ToastUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import richbar.com.richbar.R;
import richbar.com.richbar.base.BaseApplication;
import richbar.com.richbar.bean.RealNameInfoResult;
import richbar.com.richbar.bean.RealNameResult;
import richbar.com.richbar.help.CountDownTimerHelp;
import richbar.com.richbar.help.TitleHelp;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.Util;
import richbar.com.richbar.view.dialog.TipDialog;

/**
 * Created by QiYang on 2018/10/31.
 * 实名认证
 */

public class RealNameAuthenticationAc extends BaseActivity {
    @BindView(R.id.real_name)
    EditText realName;
    @BindView(R.id.tel)
    EditText tel;
    @BindView(R.id.code)
    EditText code;
    @BindView(R.id.id_card)
    EditText idCard;
    @BindView(R.id.get_code)
    TextView getCode;
    @BindView(R.id.tv3)
    TextView tv3;
    @BindView(R.id.line3)
    View line3;
    private TitleHelp titleHelp;
    @Override
    public int initLayout() {
        return R.layout.ac_real_name_authentication;
    }

    @Override
    public void initTitle() {
        titleHelp = new TitleHelp(this);
        titleHelp.setTitle("实名认证");
    }

    @Override
    public void initView() {
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        getStatus();
    }

    private void getStatus() {
        NetWorkUtil.status(BaseApplication.uid, Util.md5(), new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                LogUtil.e("WWWQQQQQQ", data);
                final RealNameResult result = MyGson.getInstance().fromJson(data, RealNameResult.class);
                setStatus(result);
            }
        });
    }

    private void setStatus(RealNameResult result) {
        if (checkIsOk(result)) {
            if (result.getData().getStatus() == 0){
                line3.setVisibility(View.VISIBLE);
                tv3.setVisibility(View.VISIBLE);
                code.setVisibility(View.VISIBLE);
                getCode.setVisibility(View.VISIBLE);
                titleHelp.setRightTv(getString(R.string.finish), new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        submit();
                    }
                });
            }else {
                getInfo(result.getData().getStatus());

            }
        }
    }

    private void getInfo(int status) {
        NetWorkUtil.getRealNameInfo(BaseApplication.uid, Util.md5(), new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                LogUtil.e("OOOAOOOOAOAO",data);
                final RealNameInfoResult result = MyGson.getInstance().fromJson(data, RealNameInfoResult.class);
                setInfo(result);
            }
        });
    }

    private void setInfo(RealNameInfoResult result) {
        if (checkIsOk(result)){
            if (null != result.getData()){
                realName.setText(result.getData().getName());
                tel.setText(result.getData().getMobile());
                idCard.setText(result.getData().getIdnumber());
                realName.setFocusable(false);
                tel.setFocusable(false);
                idCard.setFocusable(false);
                switch (result.getData().getStatus()){
                    case 1:
                        titleHelp.setRightTv("待审核");
                        break;
                    case 2:
                        titleHelp.setRightTv("已通过");
                        break;
                    case 3:
                        titleHelp.setRightTv("已拒绝");
                        break;
                }
            }
        }
    }

    private void submit() {
        NetWorkUtil.apply(BaseApplication.uid, StringUtil.getStr(realName), StringUtil.getStr(tel), StringUtil.getStr(code), StringUtil.getStr(idCard), Util.md5(), new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                LogUtil.e("data", data);
                final BaseResult result = MyGson.getInstance().fromJson(data, BaseResult.class);
                if (checkIsOk(result)) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            showTip();
                        }
                    });
                }
            }
        });
    }

    private void showTip() {
        TipDialog.show(RealNameAuthenticationAc.this, "恭喜，您已经通过实名认证!", new TipDialog.OnListener() {
            @Override
            public void onBesure() {
                finish();
            }
        });
    }


    @OnClick(R.id.get_code)
    public void onViewClicked() {
        new CountDownTimerHelp(RealNameAuthenticationAc.this, getCode, 6000, 1000).start();
        getCode();
    }

    private void getCode() {
        if (StringUtil.isEmpty(StringUtil.getStr(tel))) {
            ToastUtil.showToast(RealNameAuthenticationAc.this, getString(R.string.error_tel_number));
            return;
        }
        NetWorkUtil.getRegsinCode(Util.md5(), StringUtil.getStr(tel), "4", new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                LogUtil.e("IIIIIIIIIII", data);
            }
        });
    }

}
